/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.gzx.plugin.ams.modular.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzx.plugin.ams.modular.supplier.entity.CommoditySupplier;
import com.gzx.plugin.ams.modular.supplier.mapper.CommoditySupplierMapper;
import com.gzx.plugin.ams.modular.supplier.param.CommoditySupplierAddParam;
import com.gzx.plugin.ams.modular.supplier.param.CommoditySupplierEditParam;
import com.gzx.plugin.ams.modular.supplier.param.CommoditySupplierIdParam;
import com.gzx.plugin.ams.modular.supplier.param.CommoditySupplierPageParam;
import com.gzx.plugin.ams.modular.supplier.service.CommoditySupplierService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;


import java.util.List;

/**
 * 商品供应商Service接口实现类
 *
 * @author 刘志刚
 * @date  2025/02/21 17:45
 **/
@Service
@RequiredArgsConstructor
public class CommoditySupplierServiceImpl extends ServiceImpl<CommoditySupplierMapper, CommoditySupplier> implements CommoditySupplierService {

    private final CommoditySupplierMapper commoditySupplierMapper;

    @Override
    @DS("ams")
    public Page<CommoditySupplier> page(CommoditySupplierPageParam commoditySupplierPageParam) {
        QueryWrapper<CommoditySupplier> queryWrapper = new QueryWrapper<CommoditySupplier>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(commoditySupplierPageParam.getSupplierName())) {
            queryWrapper.lambda().like(CommoditySupplier::getSupplierName, commoditySupplierPageParam.getSupplierName());
        }
        if(ObjectUtil.isNotEmpty(commoditySupplierPageParam.getContactName())) {
            queryWrapper.lambda().notLike(CommoditySupplier::getContactName, commoditySupplierPageParam.getContactName());
        }
        if(ObjectUtil.isNotEmpty(commoditySupplierPageParam.getCommodityCode())) {
            queryWrapper.lambda().eq(CommoditySupplier::getCommodityCode, commoditySupplierPageParam.getCommodityCode());
        }
        if(ObjectUtil.isAllNotEmpty(commoditySupplierPageParam.getSortField(), commoditySupplierPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(commoditySupplierPageParam.getSortOrder());
            queryWrapper.orderBy(true, commoditySupplierPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(commoditySupplierPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(CommoditySupplier::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("ams")
    public void add(CommoditySupplierAddParam commoditySupplierAddParam) {
        CommoditySupplier commoditySupplier = BeanUtil.toBean(commoditySupplierAddParam, CommoditySupplier.class);
        this.save(commoditySupplier);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("ams")
    public void edit(CommoditySupplierEditParam commoditySupplierEditParam) {
        CommoditySupplier commoditySupplier = this.queryEntity(commoditySupplierEditParam.getId());
        BeanUtil.copyProperties(commoditySupplierEditParam, commoditySupplier);
        this.updateById(commoditySupplier);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("ams")
    public void delete(List<CommoditySupplierIdParam> commoditySupplierIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(commoditySupplierIdParamList, CommoditySupplierIdParam::getId));
    }

    @Override
    @DS("ams")
    public CommoditySupplier detail(CommoditySupplierIdParam commoditySupplierIdParam) {
        return this.queryEntity(commoditySupplierIdParam.getId());
    }

    @Override
    @DS("ams")
    public CommoditySupplier queryEntity(String id) {
        CommoditySupplier commoditySupplier = this.getById(id);
        if(ObjectUtil.isEmpty(commoditySupplier)) {
            throw new CommonException("商品供应商不存在，id值为：{}", id);
        }
        return commoditySupplier;
    }

    @Override
    @DS("ams")
    public List<CommoditySupplier> getAllCommodity() {
        LambdaQueryWrapper<CommoditySupplier> queryWrapper = new LambdaQueryWrapper<>();
        return commoditySupplierMapper.selectList(queryWrapper);
    }

    @Override
    public String getNameById(String id) {
        CommoditySupplier commoditySupplier = commoditySupplierMapper.selectById(id);
        return commoditySupplier.getSupplierName();
    }


}
